/*
 *  AppDataSegment.cpp
 *  TubeTruck
 *
 *  Created by Aeturnum on 6/6/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "AppDataSegment.h"

using namespace std;
using namespace TTP;

/*
unsigned short dataLength;
uint32_t ackNumber;
std::vector<byte> data;
*/

AppDataSegment::AppDataSegment() : mAckNumber(0)
{}
AppDataSegment::AppDataSegment(ack_num ackNumber, vector<byte>& data) :
mAckNumber(ackNumber), mData(data)
{}
AppDataSegment::AppDataSegment(const AppDataSegment& o) : 
mAckNumber(o.mAckNumber), mData(o.mData)
{}
AppDataSegment::~AppDataSegment() {}

unsigned short AppDataSegment::getDataLength() const {
   return (unsigned short)mData.size();
}

int AppDataSegment::getSize() const {
   return mData.size() + 2 + 4;
}

ack_num AppDataSegment::getAckNumber() const {
   return mAckNumber;
}

vector<byte> AppDataSegment::getData() const {
   return mData;
}

void AppDataSegment::setAckNumber(ack_num ackNumber) {
   mAckNumber = ackNumber;
}

void AppDataSegment::setData(vector<byte>& data) {
   mData = data;
}

void AppDataSegment::appendData(vector<byte>& data) {
   mData.insert(mData.end(), data.begin(), data.end());
}

// inherited
void AppDataSegment::fromVec(vector<byte>::const_iterator &vit, 
      const vector<byte>::const_iterator &end) {
   mData.clear();
   unsigned short tempLength = 0;
   
   unserialize<unsigned short>(vit, end, tempLength);
   unserialize<uint32_t>(vit, end, mAckNumber);

   mData.insert(mData.end(), vit, vit + tempLength);
   vit = vit + tempLength;
}

void AppDataSegment::toVec(vector<byte> &vec) const {
//   cout << "AppDataSegment Searializing: \n" << vec;
   unsigned short tempLength = (unsigned short)mData.size();
   serialize<unsigned short>(vec, tempLength);
   serialize<uint32_t>(vec, mAckNumber);
   vec.insert(vec.end(), mData.begin(), mData.end());
//   cout << "AppDataSegment Searialized: \n" << vec;
}
